# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2Json_v2 import Table2Json


class ItemAnncDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def insertItemAnnc(self, info):
        """
        添加 item_annc 信息
        :param info:
        :return:
        """
        anncTitle = info['anncTitle']
        deptId = info['deptId']
        anncPubTime = info['anncPubTime']
        anncUrl = info['anncUrl']
        anncStatus = info['anncStatus']
        anncRemark = info['anncRemark']

        sql = "insert into zzh_item_annc (annc_title,dept_id, annc_publish_time,annc_url,annc_status,annc_remark) VALUES (%s, %s, %s, %s, %s, %s)"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [anncTitle, deptId, anncPubTime, anncUrl, anncStatus, anncRemark])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def findItemAnncById(self, anncId):
        """
        根据ID查询 item_annc 信息
        :param anncId:
        :return:
        """
        sql = "select * from zzh_item_annc WHERE annc_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [anncId])
        jsons = None
        if rs:
            list0 = cur.fetchall()
            jsons = Table2Json.item_annc_list_to_json(list0)
        cur.close()
        self.conn.close()
        return jsons

    def findItemAnncByCondition(self, **kwargs):
        """
        item_annc 表的条件查询
        :param kwargs:
        :return:
        """
        deptId = kwargs['deptId']

        sql = "select * from zzh_item_annc WHERE dept_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [deptId])
        jsons = None
        if rs:
            list0 = cur.fetchall()
            jsons = Table2Json.item_annc_list_to_json(list0)
        cur.close()
        self.conn.close()
        return jsons

    def deleteItemAnnc(self, idArray):
        """
        根据ID删除信息，可以传递一个或者多个ID，ID以数组形式传递
        :param idArray:
        :return:
        """
        sql = "delete from zzh_item_annc WHERE annc_id=%s"
        cur = self.conn.cursor()
        for idd in idArray:
            rs = cur.execute(sql, [idd])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def updateItemAnnc(self, info):
        """
        更新 item_annc 表
        :param info:
        :return:
        """
        anncId = info['anncId']
        anncTitle = info['anncTitle']
        deptId = info['deptId']
        anncPubTime = info['anncPubTime']
        anncUrl = info['anncUrl']
        anncStatus = info['anncStatus']
        anncRemark = info['anncRemark']

        sql = "update zzh_item_annc set annc_title=%s,dept_id=%s,annc_publish_time=%s,annc_url=%s,annc_status=%s,annc_remark=%s " \
              " WHERE annc_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [anncTitle,deptId,anncPubTime,anncUrl,anncStatus,anncRemark, anncId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def selectItemAnnc(self, anncId):
        """
        根据ID查询，返回元组
        :param anncId:
        :return: data tuple
        """
        sql = "select * from zzh_item_annc WHERE annc_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [anncId])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr
